<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class distribute_uqcms extends control
{
    function __construct()
    {
        $this->spid = @$_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $status = isset($_GET['status']) ? $_GET['status'] : '1';
        if ($status) {
            $wsql = '';
        } else {
            $wsql = ' and d.commission_rate is NULL ';
        }
        $sql = "select g.id,g.shop_id,g.title,g.images,g.price,g.volume,g.status,d.gid,d.commission_rate,d.total_num,d.total_fee from " . table('goods') . " as g right join " . table('distribute_goods') . " as d on g.id = d.gid where g.shop_id = " . $this->spid . $wsql . " order by d.id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $result = $this->db->get_all($sql);
        if ($result) {
            foreach ($result as $k => $v) {
                $v['title'] = cut_str($v['title'], '20');
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('goods') . " where shop_id = " . $this->spid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function add()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $sql = "select g.id,g.shop_id,g.title,g.images,g.price,g.stock,g.status,d.gid,d.commission_rate,d.total_num,d.total_fee from " . table('goods') . " as g left join " . table('distribute_goods') . " as d on g.id = d.gid where g.status = 1 and g.shop_id = " . $this->spid . " and d.gid is null order by g.id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $result = $this->db->get_all($sql);
        if ($result) {
            foreach ($result as $k => $v) {
                $v['title'] = cut_str($v['title'], '20');
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('goods') . " where shop_id = " . $this->spid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function add_distribute()
    {
        $gid = isset($_POST['gid']) ? $_POST['gid'] : error_json('没有选择产品');
        $row = $this->db->get_one("select id,shop_id from " . table('goods') . " where shop_id = " . $this->spid . " and id = " . $gid);
        if ($row['id']) {
            $drow = $this->db->get_one("select id,gid from " . table('distribute_goods') . " where gid = " . $row['id']);
            if ($drow['id']) {
                $query = $this->db->delete(table('distribute_goods'), 'id = ' . $drow['id']);
                if ($query) {
                    right_json();
                } else {
                    error_json('删除失败');
                }
            } else {
                $setarr['gid'] = $gid;
                $setarr['commission_rate'] = '5';
                $setarr['addtime'] = time();
                $query = $this->db->add(table('distribute_goods'), $setarr);
                if ($query) {
                    right_json();
                } else {
                    error_json('加入失败');
                }
            }
        } else {
            error_json('取消的产品不存在');
        }
    }

    function commission_rate_save()
    {
        $gid = isset($_POST['gid']) ? $_POST['gid'] : exit('0');
        $row = $this->db->get_one("select id,shop_id from " . table('goods') . " where shop_id = " . $this->spid . " and id = " . $gid);
        if ($row['id']) {
            $setarr['commission_rate'] = isset($_POST['rate_value']) ? $_POST['rate_value'] : exit('0');
            $query = $this->db->update(table('distribute_goods'), $setarr, 'gid = ' . $row['id']);
            if ($query) {
                exit('1');
            } else {
                exit('0');
            }
        } else {
            exit('0');
        }
    }
    function bill()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $result = $this->db->get_all("select * from " . table('distribute_bill') . " where type = 1 and shop_id = " . $this->spid . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $v['title'] = cut_str($v['title'], '20');
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('distribute_bill') . " where shop_id = " . $this->spid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
}